class LastJiFenPersonCell extends eui.ItemRenderer{
    public static WIDTH:number = 0;

    private _bg:egret.Bitmap; // 背景
    private _indexImg:egret.Bitmap; // 排名
    private _headerImg:CellHeaderView; // 头像等级
    private _nicknameLabel:BaseTextField; // 昵称
    private _bangImg:egret.Bitmap; // 帮会图标
    private _bangnameLabel:BaseTextField; // 帮会名
    private _codeBg:egret.Bitmap; // 积分背景
    private _codeTitleLabel:BaseTextField; // 积分标题
    private _codeLabel:BaseTextField; // 积分
    private _awardsView:LastJiFenCellAwardView; // 显示奖品

    constructor(){
        super();
        this.height = this.minCellHeight;
        this.width = LastJiFenPersonCell.WIDTH;
    }

    protected dataChanged():void{
        var data = this.data;
        var index = data['rank'];
        // 1. 背景
        this.bg.texture = RES.getRes('hi_rank_cell' + index);
        // 2. 名次
        this.indexImg.texture = RES.getRes(index.toString());
        // 3.头像等级
        this.headerImg.iconURL = data['head_url'];
        this.headerImg.level = data['level_no'];
        // 4.昵称
        this.nicknameLabel.text = data['nickname'];
        // 5.帮会名
        this.bangnameLabel.text = data['gang_name'] || "无";
        // 6.积分
        this.codeLabel.text = data['total_score'];
        var realCodeWidth = this.codeLabel.strWidth;
        if(realCodeWidth + 2 * 10 > this.minCodeBgWidth){
            this.updateCode(realCodeWidth + 2 * 10);
        }else{
            this.updateCode(this.minCodeBgWidth);
        }
        // 积分宽度确定了 那么帮派名和人名的宽度也确定了
        this.updateNameFrames();

        // 7.奖品
        var awards = data['props'];
        // 8. 根据奖品数量自适应高度
        if(awards == undefined || awards.length == 0){
            this.height = this.minCellHeight;
            if(this.contains(this.awardsView)) this.removeChild(this.awardsView);
        }else{
            if(!this.contains(this.awardsView)) this.addChild(this.awardsView);
            this.awardsView.awards = awards;
            this.awardsView.reloadData();
            this.height = this.minCellHeight + 20 + this.awardsView.height;
        }
        this.bg.height = this.bgH;
        
    }

    protected createChildren(){
        // 背景
        if(!this.contains(this.bg)) this.addChild(this.bg);
        // 名次
        if(!this.contains(this.indexImg)) this.addChild(this.indexImg);
        // 头像
        if(!this.contains(this.headerImg)) this.addChild(this.headerImg);
        // 昵称
        if(!this.contains(this.nicknameLabel)) this.addChild(this.nicknameLabel);
        // 积分背景
        if(!this.contains(this.codeBg)) this.addChild(this.codeBg);
        // 帮会图标
        if(!this.contains(this.bangImg)) this.addChild(this.bangImg);
        // 帮会名
        if(!this.contains(this.bangnameLabel)) this.addChild(this.bangnameLabel);
        // 积分标题
        if(!this.contains(this.codeTitleLabel)) this.addChild(this.codeTitleLabel);
        // 积分
        if(!this.contains(this.codeLabel)) this.addChild(this.codeLabel);
    }

    /*********************************************** private **********************************************************/
    // 自适应宽度积分
    private updateCode(width:number){
        this.codeLabel.width = width;
        this.codeBg.width = this.codeLabel.width;
        this.codeTitleLabel.width  = this.codeLabel.width;
        this.codeBg.x = this.width  - this.codeBg.width - 10;
        this.codeTitleLabel.x = this.codeBg.x;
        this.codeLabel.x = this.codeBg.x;
    }

    private updateNameFrames(){
        this.nicknameLabel.width = this.codeBg.x - this.nicknameLabel.x - 10;
        this.bangnameLabel.width = this.codeBg.x - this.bangnameLabel.x - 10;
    }

    /*********************************************** getter **********************************************************/
    
    private get awardsView():LastJiFenCellAwardView{
        if(!this._awardsView){
            var awardsView = new LastJiFenCellAwardView();
            awardsView.x = 10;
            awardsView.width = this.width - 2 * awardsView.x;
            awardsView.y = utils.getMaxY(this.indexImg) + 20;
            this._awardsView = awardsView;
        }
        return this._awardsView;
    }

    private get minCellHeight():number{
        return 160;
    }

    public get codeLabel():BaseTextField{
        if(!this._codeLabel){
            var codeLabel = new BaseTextField();
            codeLabel.textColor = 0xFED253;
            codeLabel.size = 28;
            codeLabel.width = this.codeBg.width;
            codeLabel.height = 30;
            codeLabel.textAlign = egret.HorizontalAlign.CENTER;
            codeLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            codeLabel.italic = true;
            codeLabel.bold = true;
            codeLabel.x = this.codeBg.x;
            codeLabel.y = this.codeBg.y;
            this._codeLabel = codeLabel;
        }
        return this._codeLabel;
    }

    public get codeTitleLabel():BaseTextField{
        if(!this._codeTitleLabel){
            var codeTitleLabel = new BaseTextField();
            codeTitleLabel.textColor = 0xffffff;
            codeTitleLabel.size = 22;
            codeTitleLabel.width = this.codeBg.width;
            codeTitleLabel.text = "个人积分";
            codeTitleLabel.textAlign = egret.HorizontalAlign.CENTER;
            codeTitleLabel.x = this.codeBg.x;
            codeTitleLabel.y = this.codeBg.y - codeTitleLabel.height - 5;
            codeTitleLabel.italic = true;
            this._codeTitleLabel = codeTitleLabel;
        }
        return this._codeTitleLabel;
    }

    public get codeBg():egret.Bitmap{
        if(!this._codeBg){
            var codeBg = utils.createBitmapByName('jfbg', {offsetY: 0.5});
            codeBg.scale9Grid = new egret.Rectangle(26, 26, 72, 36);
            codeBg.x = this.width - codeBg.width - 10;
            codeBg.y = this.indexImg.y;
            this._codeBg = codeBg;  
        }
        return this._codeBg;
    }

    private get minCodeBgWidth():number{
        return 126;
    }

    public get bangnameLabel():BaseTextField{
        if(!this._bangnameLabel){
            var bangnameLabel = new BaseTextField();
            bangnameLabel.textColor = 0xFFFFFF;
            bangnameLabel.stroke = 2;
            bangnameLabel.strokeColor = 0x6D410B;
            bangnameLabel.height = 30;
            bangnameLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            bangnameLabel.anchorOffsetY = bangnameLabel.height / 2.0;
            bangnameLabel.x = utils.getMaxX(this.bangImg) + 5;
            bangnameLabel.y = this.bangImg.y;
            bangnameLabel.size = 24;
            bangnameLabel.bold = true;
            this._bangnameLabel = bangnameLabel;
        }
        return this._bangnameLabel;
    }

    public get bangImg():egret.Bitmap{
        if(!this._bangImg){
            var bangImg = utils.createBitmapByName('bhicon', {offsetY: 0.5});
            bangImg.x = this.headerImg.x;
            bangImg.y = this.indexImg.y + bangImg.anchorOffsetY + 5 + 10;
            this._bangImg = bangImg;
        }
        return this._bangImg;
    }

    public get nicknameLabel():BaseTextField{
        if(!this._nicknameLabel){
            var nicknameLabel = new BaseTextField();
            nicknameLabel.height = 30;
            nicknameLabel.verticalAlign = egret.VerticalAlign.MIDDLE;
            nicknameLabel.anchorOffsetY = nicknameLabel.height / 2.0;
            nicknameLabel.textColor = 0x6C400B;
            nicknameLabel.x = utils.getMaxX(this.headerImg) + 5;
            nicknameLabel.y = this.headerImg.y;
            nicknameLabel.bold = true;
            nicknameLabel.size = 26;
            this._nicknameLabel = nicknameLabel;
        }
        return this._nicknameLabel;
    }

    public get headerImg():CellHeaderView{
        if(!this._headerImg){
            var headerImg = new CellHeaderView();
            headerImg.width = headerImg.height = 60;
            headerImg.anchorOffsetY = headerImg.height / 2.0;
            headerImg.x = utils.getMaxX(this.indexImg) + 5;
            headerImg.y = this.indexImg.y + 10 - headerImg.height + headerImg.anchorOffsetY;
            this._headerImg = headerImg;
        }
        return this._headerImg;
    }

    public get indexImg():egret.Bitmap{
        if(!this._indexImg){
            var indexImg = utils.createBitmapByName('1', {offsetY: 0.5});
            indexImg.x = 10;
            indexImg.y = 80;
            this._indexImg = indexImg;
        }
        return this._indexImg;
    }

    public get bg():egret.Bitmap{
        if(!this._bg){
            var bg = utils.createBitmapByName("hi_rank_cell3", {width:this.width});
            bg.scale9Grid = new egret.Rectangle(158, 14, 206, 24);
            bg.y = this.bgY;
            bg.height = this.bgH;
            this._bg = bg;
        }
        return this._bg;
    }

    private get bgH():number{
        return this.height - 2 * this.bgY;
    }

    private get bgY():number{
        return 5;
    }
}
